package com.maxd.modules.selfshop.merchants.service;

import com.maxd.error.Result;
import com.maxd.error.ResultUtil;
import com.maxd.model.Goods;
import com.maxd.modules.selfshop.merchants.entity.SelfStore;
import com.maxd.modules.selfshop.merchants.entity.SelfStoreCollect;
import com.maxd.modules.selfshop.merchants.respository.SelfStoreCollectJpaRepository;
import com.maxd.modules.selfshop.merchants.respository.SelfStoreJpaRepository;
import com.maxd.respository.selfGoods.GoodsJpaRepository;
import com.maxd.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

@Service
public class SelfStoreCollectServiceImpl implements SelfStoreCollectService {
    @Autowired
    private SelfStoreCollectJpaRepository jpaRepository;
    @Autowired
    private SelfStoreJpaRepository selfStoreJpaRepository;
    @Autowired
    private GoodsJpaRepository goodsJpaRepository;


    @Override
    public Result findAll(Integer page, Integer size, Long userId) {
        Pageable pageable = PageRequest.of(page, size, Sort.by(new Sort.Order(Sort.Direction.DESC, "createTime")));
        Specification<SelfStoreCollect> queryCondition = new Specification<SelfStoreCollect>() {
            @Override
            public Predicate toPredicate(Root<SelfStoreCollect> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicateList = new ArrayList<>();
                predicateList.add(criteriaBuilder.equal(root.get("userId"), userId));
                return criteriaBuilder.and(predicateList.toArray(new Predicate[predicateList.size()]));
            }
        };
        Page<SelfStoreCollect> all = jpaRepository.findAll(queryCondition, pageable);
        List<SelfStore> allStore = selfStoreJpaRepository.findAll();
        List<Goods> allGoods = goodsJpaRepository.findAll();
        for (SelfStoreCollect c : all.getContent()) {
            for (SelfStore s : allStore) {
                if (c.getStoreId().equals(s.getStoreId())){
                    c.setStore(s);
                    break;
                }
            }
            List<Goods> goodsList = new ArrayList<>();
            for (Goods g : allGoods) {
                if (c.getStoreId().equals(g.getStoreId())){
                    goodsList.add(g);
                }
            }
            c.setGoodsList(goodsList);
        }
        return ResultUtil.success(all);
    }

    @Override
    public Result saveBody(SelfStoreCollect entity) {
        SelfStoreCollect s = jpaRepository.findByStoreIdAndUserId(entity.getStoreId(), entity.getUserId());
        if (s != null){
            return ResultUtil.error(-1,"重复收藏");
        }
        entity.setCreateTime(DateUtil.createTime()); //创建时间
        return ResultUtil.success(jpaRepository.save(entity));
    }

    @Override
    public Result updateBody(SelfStoreCollect entity) {
        return ResultUtil.success(jpaRepository.save(entity));
    }

    @Override
    public Result findOne(Long id) {
        return ResultUtil.success(jpaRepository.findById(id).orElse(null));
    }

    @Override
    public Result delete(Long id) {
        jpaRepository.deleteById(id);
        return ResultUtil.success();
    }

    @Override
    public Result check(Long storeId, Long userId) {
        SelfStoreCollect s = jpaRepository.findByStoreIdAndUserId(storeId, userId);
        if (s != null && s.getId() != null){
            Result result = new Result();
            result.setData(false);
            result.setMsg(s.getId().toString());
            result.setStatus(0);
            return result;
        }
        return ResultUtil.success(true);
    }
}
